use crate::error::Result;
use crate::llvm_backend::LLVMBackend;
use inkwell::values::BasicValueEnum;

impl<'ctx> LLVMBackend<'ctx> {


    /// Generate is_ok() method - returns true if Result is Ok
    pub(super) fn generate_result_is_ok(
        &mut self,
        result_value: BasicValueEnum<'ctx>,
    ) -> Result<BasicValueEnum<'ctx>> {
        let tag = self.extract_result_tag(result_value)?;
        let zero = self.context.i32_type().const_int(0, false);
        let is_ok =
            self.builder
                .build_int_compare(inkwell::IntPredicate::EQ, tag, zero, "is_ok")?;
        Ok(is_ok.into())
    }


    /// Generate is_err() method - returns true if Result is Err
    pub(super) fn generate_result_is_err(
        &mut self,
        result_value: BasicValueEnum<'ctx>,
    ) -> Result<BasicValueEnum<'ctx>> {
        let tag = self.extract_result_tag(result_value)?;
        let one = self.context.i32_type().const_int(1, false);
        let is_err =
            self.builder
                .build_int_compare(inkwell::IntPredicate::EQ, tag, one, "is_err")?;
        Ok(is_err.into())
    }

}
